package Gerrit::API;
use 5.018;
use warnings;
use List::Util qw(min);
use Mojo::UserAgent;
use Mojo::JSON qw(decode_json true false);
my $ua = Mojo::UserAgent->new;
my $url_base = "https://cr.deepin.io";
use Mojo::URL;
use Mojo::Parameters;
use Carp qw(carp);

sub getChangeUrl
{
	my $cl_num = $_[0];
	return "$url_base/#/c/$cl_num";
}

sub wrapGerritTX
{
	my ($ua, $tx) = @_;
	my $ret;
	if (my $res = $tx->success)
	{
		my $body = $res->body;
		#warn $body;
		my $result = decode_json( substr( $body, index ($body, "\n") + 1) );
		$ret = {error => false, result => $result };
	}
	else
	{
		my $err = $tx->error;
		my $err_str = "$err->{code} : $err->{message}";
		carp 'ERROR: '.$err_str;
		$ret = {error => true, error_message => $err_str };
	}
	return $ret;

}

#param (clnum, o => 'DETAIL',  o => 'LABELS', sub {});
sub getChangeInfo
{
	my $cl_num = shift;
	my $cb = pop;
	my $url = Mojo::URL->new("$url_base/changes/$cl_num");
	$url->query(Mojo::Parameters->new( @_ ));
	warn "getChangeInfo url: $url";
	$ua->get($url => sub {
		$cb->(wrapGerritTX(@_));
	})
}

sub getChangeCommitMessage
{
	my ($cl_num, $cb) = @_;
	warn 'getChangeCommitMessage CL '. $cl_num;
	getChangeInfo($cl_num, o => 'CURRENT_COMMIT', o => 'CURRENT_REVISION', sub {
		my ($ret) = @_;
		
		unless ($ret->{error})
		{
			#warn Dumper $ret->{result};
			my $info = $ret->{result};
			my $current_revision = $info->{current_revision};
			my $message = $info->{revisions}{ $current_revision }{commit}{message};
			#warn 'commit message is '. $message;
			$ret->{result} = $message;
		}
		$cb->($ret);
	});
}

#get detail include all comment
sub getChangeDetail
{
	my ($cl_num, $cb) = @_;
	my $url = "$url_base/changes/$cl_num/detail";
	$ua->get($url => sub {
		$cb->(wrapGerritTX(@_));
	})
}

# getChanges(q => '', n => '', sub {} );
sub getChanges
{
	my $cb = pop;
	my $url = Mojo::URL->new("$url_base/changes/");
	$url->query(Mojo::Parameters->new(@_));
	warn "getChanges url: $url";
	#like https://cr.deepin.io/changes/?q=status:open
	$ua->get($url => sub {
		$cb->(wrapGerritTX(@_));
	});
}

#example:
# getChangeDetail(123, sub {
# 	my ($ret )= @_;
# 	my $detail = $ret->{result};
#	my $v_value = getChangeVerifiedValue($detail);
# })
sub getChangeVerifiedValue
{
	my ($detail) = @_;
	my $all_verified = $detail->{labels}{Verified}{all};
	my $verified_value = 0;
	my $v_ok = 0;
	my $v_failed = 0;
	
	for (@$all_verified)
	{
		my $name = $_->{name};
		my $value = $_->{value};
		next if ($name eq 'jenkins' && $value > 0 );

		warn $_->{name} . ' give value '. $_->{value};

		$v_ok = 1 if $value > 0;
		$v_failed = 1 if $value < 0;
	}
	if ($v_failed)
	{
		$verified_value = -1;
	}
	elsif ( $v_ok )
	{
		$verified_value = 1;
	}
	else
	{
		$verified_value = 0;
	}
	warn 'getChangeVerifiedValue: verified value is '. $verified_value;
	return $verified_value;
}


sub getChangeByCommitId
{
	my ($commit_id, $cb) = @_;
	getChanges(q => 'commit:'. $commit_id, sub {
		my ($ret) = @_;
		unless($ret->{error})
		{
			$ret->{result} = $ret->{result}[0];
		}
		else
		{
			warn "getChangeByCommitId failed";
		}
		$cb->($ret);
	});
}


1;
